package com.Base.NetBase.Socket
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import com.Base.Logger.Logger;
	
	public class MessagePackage
	{
		public function MessagePackage():void
		{
		}
		
		//取得消息参数
		public function getParam(index:int):*
		{
			var param:* = null;
			
			if (checkData(index))
			{
				param = _data[index];
			}
			
			return param;
		}
		
		//设置数据包的内容
		public function set data(bytes:ByteArray):void
		{
			_data = bytes.readObject() as Array;
		}
		
		//将数组打包成包含信息头的二进制数据
		public static function pack(data:Array):ByteArray
		{
			var dataBytes:ByteArray = new ByteArray();
			var fullBytes:ByteArray = new ByteArray();

			dataBytes.writeObject(data);		
			fullBytes.writeUnsignedInt(dataBytes.length);
			fullBytes.writeBytes(dataBytes);
			fullBytes.position = 0;
			//fullBytes.endian = Endian.LITTLE_ENDIAN;
			
			return fullBytes;
		}
		
		//检查数据是否存在
		private function checkData(index:int):Boolean
		{
			var ret:Boolean = true;
			
			if (null == _data)
			{
				Logger.throwError("MessagePackage::checkData()-->Data doesn't exists!");
				ret = false;
			}
			else if (index >= _data.length)
			{
				Logger.throwError("MessagePackage::checkData()-->Wrong index!");
				ret = false;
			}
			
			return ret;
		}
		
		private const MSG_HEAD_LENGTH:int = 4;
		
		private var _data:Array;
	}
}